{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6df34a61-7103-41d0-a888-39378350ec1e",
   "metadata": {},
   "source": [
    "智建2302 姚龙飞 python作业"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0ab734d-5d3f-4e24-8505-e0eb0d3d8bb0",
   "metadata": {},
   "source": [
    "1.输入某年某月某日，判断这一天是这一年的第几天?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f39f4955-dc26-4071-881b-055c1e998540",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入年份:  2024\n",
      "请输入月份:  3\n",
      "请输入日期:  21\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024-3-21 是这一年的第 81 天。\n"
     ]
    }
   ],
   "source": [
    "from datetime import datetime\n",
    "\n",
    "def day_of_year(year, month, day):\n",
    "    date = datetime(year, month, day)\n",
    "    return (date - datetime(year, 1, 1)).days + 1\n",
    "\n",
    "year = int(input(\"请输入年份: \"))\n",
    "month = int(input(\"请输入月份: \"))\n",
    "day = int(input(\"请输入日期: \"))\n",
    "\n",
    "print(f\"{year}-{month}-{day} 是这一年的第 {day_of_year(year, month, day)} 天。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2ae78e5-3174-46e1-ba4f-a400a2cac4f0",
   "metadata": {},
   "source": [
    "3.判断1000中有多少个素数，并输出所有素数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "326412b6-cf38-492e-96ce-0c82ace2a4bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "168\n",
      "[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def is_prime(n):\n",
    "    if n <= 1:\n",
    "        return False\n",
    "    if n <= 3:\n",
    "        return True\n",
    "    if n % 2 == 0 or n % 3 == 0:\n",
    "        return False\n",
    "    i = 5\n",
    "    while i * i <= n:\n",
    "        if n % i == 0 or n % (i + 2) == 0:\n",
    "            return False\n",
    "        i += 6\n",
    "    return True\n",
    "\n",
    "primes = []\n",
    "for num in range(2, 1001):\n",
    "    if is_prime(num):\n",
    "        primes.append(num)\n",
    "\n",
    "prime_count = len(primes)\n",
    "print(prime_count)\n",
    "print(primes)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "893a755c-8b4d-47a2-84c2-41c7bd124002",
   "metadata": {},
   "source": [
    "5. 利用条件运算符的嵌套来完成此题： 学习成绩>=90分的同学用A表示， 60-89分之间的用B表示， 60分以下的用C表示。 (要求有键盘输入)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "861baecc-cedb-4d48-a2a3-b17f42e906b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入学生成绩： 66\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "B\n"
     ]
    }
   ],
   "source": [
    "score = float(input(\"请输入学生成绩：\"))\n",
    "\n",
    "if score >= 90:\n",
    "    grade = 'A'\n",
    "elif score >= 60:\n",
    "    grade = 'B'\n",
    "else:\n",
    "    grade = 'C'\n",
    "\n",
    "print(grade)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bfdfb15-7d33-48f8-b262-42bfcc838076",
   "metadata": {},
   "source": [
    "7.  输入一行字符串， 分别统计出其中英文字母、空格、数字和其它字符的个数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "fe63e049-31c9-4742-9c80-97d8498a110f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一行字符串： asdjkhkad 123hjkhqa\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数： 15\n",
      "空格个数： 1\n",
      "数字个数： 3\n",
      "其他字符个数： 0\n"
     ]
    }
   ],
   "source": [
    "input_str = input(\"请输入一行字符串：\")\n",
    "letter_count = 0\n",
    "space_count = 0\n",
    "digit_count = 0\n",
    "other_count = 0\n",
    "\n",
    "for char in input_str:\n",
    "    if char.isalpha():\n",
    "        letter_count += 1\n",
    "    elif char.isspace():\n",
    "        space_count += 1\n",
    "    elif char.isdigit():\n",
    "        digit_count += 1\n",
    "    else:\n",
    "        other_count += 1\n",
    "\n",
    "print(\"英文字母个数：\", letter_count)\n",
    "print(\"空格个数：\", space_count)\n",
    "print(\"数字个数：\", digit_count)\n",
    "print(\"其他字符个数：\", other_count)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d0e8fea-06e1-4172-b65d-fdaeef918833",
   "metadata": {},
   "source": [
    "9.一个数如果恰好等于它的因子之和， 这个数就称为“完数”。例如6=1+2+3.编程， 找出1000以内的所有完数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "34e988ab-2156-4550-b2a7-b3c8605bbf61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的完数有： [6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "def find_perfect_numbers(limit):\n",
    "    perfect_numbers = []\n",
    "    for num in range(1, limit):\n",
    "        sum_of_factors = 0\n",
    "        for i in range(1, num):\n",
    "            if num % i == 0:\n",
    "                sum_of_factors += i\n",
    "        if sum_of_factors == num:\n",
    "            perfect_numbers.append(num)\n",
    "    return perfect_numbers\n",
    "\n",
    "perfect_numbers = find_perfect_numbers(1000)\n",
    "print(\"1000以内的完数有：\", perfect_numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f4272474-f9fe-4368-b709-c4c96e9fa899",
   "metadata": {},
   "source": [
    "11. 输入任意数N， 求1+2!+3!+…+N!！的值 (注意实数范围)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b58c18b2-cc2e-4ffb-a59f-720240086511",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入任意数N： 4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1+2!+3!+…+4!的值为：33\n"
     ]
    }
   ],
   "source": [
    "def factorial(n):\n",
    "    if n == 0:\n",
    "        return 1\n",
    "    else:\n",
    "        return n * factorial(n-1)\n",
    "\n",
    "N = int(input(\"请输入任意数N：\"))\n",
    "sum_of_factorials = sum(factorial(i) for i in range(1, N+1))\n",
    "print(f\"1+2!+3!+…+{N}!的值为：{sum_of_factorials}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8edb5614-cce4-4de8-a8f9-d66c4c13b59b",
   "metadata": {},
   "source": [
    "13. 输入任意奇数N， 按如下要求打印出图案 (菱形)***************(N控制行数和本行的星数)********"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "e4c2b415-06b2-46a6-bc8a-ae665312ad56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个奇数N： 9\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n",
      "***\n",
      "*****\n",
      "*******\n",
      "*********\n",
      "*******\n",
      "*****\n",
      "***\n",
      "*\n"
     ]
    }
   ],
   "source": [
    "N = int(input(\"请输入一个奇数N：\"))\n",
    "\n",
    "# 打印上半部分包括中间行\n",
    "for i in range(1, N + 1, 2):\n",
    "    print('*' * i)\n",
    "\n",
    "# 打印下半部分\n",
    "for i in range(N - 2, 0, -2):\n",
    "    print('*' * i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4c5ada0e-4c77-4902-9640-2b3ad9ed8ca0",
   "metadata": {},
   "source": [
    "15.对10个数进行排序。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "da230787-13ac-48c4-a968-5ba7cdc08957",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入10个数，用空格分隔： 12 32 412 12 523 33 44 55 11\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后的数列为： [11, 12, 12, 32, 33, 44, 55, 412, 523]\n"
     ]
    }
   ],
   "source": [
    "numbers = [int(x) for x in input(\"请输入10个数，用空格分隔：\").split()]\n",
    "numbers.sort()\n",
    "print(\"排序后的数列为：\", numbers)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8266de2b-764f-4a0c-865e-2830972e6117",
   "metadata": {},
   "source": [
    "17．有一个已经排好序的数组（元素数量大于10）。从键盘任意输入一个数，要求按原来的规律将它插入数组中。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "2f90174e-d5f0-4c62-807b-91fb42a1042b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入一个数： 23\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入新数后的数组为： [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23]\n"
     ]
    }
   ],
   "source": [
    "sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21]\n",
    "new_number = int(input(\"请输入一个数：\"))\n",
    "\n",
    "insert_position = 0\n",
    "for i in range(len(sorted_array)):\n",
    "    if sorted_array[i] > new_number:\n",
    "        insert_position = i\n",
    "        break\n",
    "else:\n",
    "    insert_position = len(sorted_array)\n",
    "\n",
    "sorted_array.insert(insert_position, new_number)\n",
    "\n",
    "print(\"插入新数后的数组为：\", sorted_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0dcabd5b-2ecd-47bb-9e13-0ec75d94f1db",
   "metadata": {},
   "source": [
    "19．有n个人围成一圈，顺序排号。从第一个人开始报数（从1到3报数），凡报到3的人退出圈子，问最后留下的是原来第几号的那位。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "96153d50-0e3e-40a1-a074-5e4306873336",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入人数n： 23\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第8号的那位。\n"
     ]
    }
   ],
   "source": [
    "def last_remaining(n):\n",
    "    people = list(range(1, n + 1))\n",
    "    index = 0\n",
    "    while len(people) > 1:\n",
    "        index = (index + 2) % len(people)  \n",
    "        people.pop(index)\n",
    "    return people[0]\n",
    "\n",
    "n = int(input(\"请输入人数n：\"))\n",
    "print(\"最后留下的是原来第{}号的那位。\".format(last_remaining(n)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
